﻿#include "roleform.h"
#include "ui_roleform.h"

RoleForm::RoleForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RoleForm)
{
    ui->setupUi(this);
    m_pSqlQueryModel = new QSqlQueryModel(this);

    connect(ui->m_btnAdd, &QPushButton::clicked, this, &RoleForm::on_add);
    connect(ui->m_btnEdit, &QPushButton::clicked, this, &RoleForm::on_edit);
    connect(ui->m_btnDelete, &QPushButton::clicked, this, &RoleForm::on_delete);

    connect(ui->m_btnQuery, &QPushButton::clicked, this, &RoleForm::init);
}

RoleForm::~RoleForm()
{
    delete ui;  
}

void RoleForm::init()
{
    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if(database.isOpen())
    {
        qDebug() << "open";

        QString sql = QString("select * from t_role where role_name like '%%1%'")
                .arg(ui->m_roleName->text());
        qDebug() << sql;

        m_pSqlQueryModel->setQuery(sql, database);
        m_pSqlQueryModel->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit("主键"));
        m_pSqlQueryModel->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit("角色名"));
        ui->m_tableViewRole->setModel(m_pSqlQueryModel);
    }
    else
    {
        qDebug() << "open fail";
    }
}

void RoleForm::on_add()
{
    RoleDialog roleDialog;
    Role role;
    role.op = "ADD";
    roleDialog.init(role);
    if(roleDialog.exec() == QDialog::Accepted)
    {
        init();
    }
}

void RoleForm::on_edit()
{
    Role role;
    role.op = "EDIT";

    int row= ui->m_tableViewRole->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::information(this,
                                 QString::fromLocal8Bit("提示"),
                                 QString::fromLocal8Bit("请选择修改项"));
        return;
    }

    QAbstractItemModel *model = ui->m_tableViewRole->model();
    QModelIndex index = model->index(row, 0);//选中行第一列的内容
    QVariant data = model->data(index);
    role.roleId = data.toLongLong();
    index = model->index(row, 1);
    data = model->data(index);
    role.roleName = data.toString();

    RoleDialog roleDialog;
    roleDialog.init(role);

    if(roleDialog.exec() == QDialog::Accepted)
    {
        init();
    }
}

void RoleForm::on_delete()
{
    int row= ui->m_tableViewRole->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::information(this,
                                 QString::fromLocal8Bit("提示"),
                                 QString::fromLocal8Bit("请选择删除项"));
        return;
    }

    if(QMessageBox::question(this,
                             QString::fromLocal8Bit("提示"),
                             QString::fromLocal8Bit("请确认删除"),
                             QString::fromLocal8Bit("取消"),
                             QString::fromLocal8Bit("确认")) != 1)
    {
        return;
    }

    QAbstractItemModel *model = ui->m_tableViewRole->model();
    QModelIndex index = model->index(row,0);//选中行第一列的内容
    QVariant data = model->data(index);

    QString sql = QString("delete from t_role where role_id = %1")
            .arg(data.toLongLong());
    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if (database.open())
    {
        QSqlQuery sql_query(database);
        if(sql_query.exec(sql))
        {
            init();
            qDebug() << "delete t_role" << sql;
        }
    }
}
